package com.kevin.Code.Tree;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Vinlee Xiao
 * @Classname BinaryTreeRightSideView
 * @Description Leetcode 199. 二叉树的右视图 中等难度 没啥思路 高频 一刷
 * @Date 2021/8/11 16:44
 * @Version 1.0
 */
public class BinaryTreeRightSideView {
    public static void main(String[] args) {

    }


    /**
     * 广度优先遍历
     *
     * @param root
     * @return
     */
    public List<Integer> rightSideView(TreeNode root) {

        //特殊情况为空
        if (root == null) {
            return new ArrayList<>();
        }
        //广度优先遍历
        Deque<TreeNode> deque = new LinkedList<>();
        List<Integer> result = new ArrayList<>();
        deque.addLast(root);
        int depth = 0;
        while (!deque.isEmpty()) {

            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.pollFirst();
                if (i == size - 1) {
                    result.add(node.val);
                }
                if (node.left != null) {
                    deque.addLast(node.left);
                }

                if (node.right != null) {
                    deque.addLast(node.right);
                }
            }

        }

        return result;
    }

}
